import 'package:flutter/material.dart';
import 'package:flutter_app/api/api.dart' as api;
import 'package:flutter_app/bean/respon.dart';
import 'package:flutter_app/ui/pages/content.dart';

class CollectionPage extends StatefulWidget {
  @override
  CollectionPageState createState() {
    return new CollectionPageState();
  }

  const CollectionPage();
}

class CollectionPageState extends State<CollectionPage> {
  List<dynamic> _data;
  int _page = 0;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text("收藏中心"),
      ),
      body: new ListView.builder(
          itemCount: _data == null ? 0 : _data.length,
          itemBuilder: (ctx, index) {
            return Card(
              child: GestureDetector(
                child: Container(
                  padding: const EdgeInsets.all(5.0),
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text("文章标题: " + _data[index]['title']),
                      Text("作者姓名: " + _data[index]['author']),
                      Text("文章种类: " + _data[index]['chapterName']),
                      Text("简单描述: " + _data[index]['desc']),
                      Text("获赞数量: " + _data[index]['zan'].toString()),
                    ],
                  ),
                ),
                onTap: () {
                  Navigator
                      .of(context)
                      .push(new MaterialPageRoute(builder: (ctx) {
                    var info = new OneInfo.fromJson(_data[index]);
                    return new ContentPage(info);
                  }));
                },
                onLongPress: () {
                  api.unCollection(_onUnCollection, _data[index]['originId']);
                },
              ),
            );
          }),
    );
  }

  @override
  void initState() {
    super.initState();
    _getData();
  }

  void _getData() {
    api.getCollectionList(_onDataLoad, _page);
  }

  void _onDataLoad(data) {
    print(data);
    setState(() {
      this._data = data;
    });
  }

  void _onUnCollection(data) {
    print(data);
    api.getCollectionList(_onDataLoad, _page);
  }
}
